import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.glu.GLU;

/**
 * Created by WheelChen on 2017/5/19.
 */
public class Polygon implements GLEventListener{
    private GLU glu = new GLU();

    @Override
    public void init(GLAutoDrawable glAutoDrawable) {
        GL2 gl = glAutoDrawable.getGL().getGL2();
//        gl.glClearColor(1.0f, 0f, 0f, 0.5f);
    }

    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) {

    }

    @Override
    public void display(GLAutoDrawable glAutoDrawable) {
        System.out.println("绘制中...");
        final GL2 gl = glAutoDrawable.getGL().getGL2();
        gl.glScalef( 0.50f,0.25f,0.50f );

//        gl.glTranslated(0f,0f,-2f);

        gl.glBegin(gl.GL_POLYGON);
        gl.glColor3f( 1.0f, 0.0f, 0.0f ); //Red
        gl.glVertex3f(0f,0.5f,0f);

        gl.glColor3f( 0.0f,1.0f,0.0f ); //blue
        gl.glVertex3f(-0.5f,0.2f,0f);

        gl.glColor3f( 0.0f,0.0f,1.0f ); //green
        gl.glVertex3f(-0.5f,-0.2f,0f);
        gl.glVertex3f(0f,-0.5f,0f);
        gl.glVertex3f(0f,0.5f,0f);
        gl.glVertex3f(0.5f,0.2f,0f);
        gl.glVertex3f(0.5f,-0.2f,0f);

        gl.glColor3f( 0.0f,0.0f,1.0f ); //green
        gl.glVertex3f(0f,-0.5f,0f);


        gl.glEnd();


    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x,  int y, int width, int height) {
        System.out.println("变换");
        final GL2 gl = drawable.getGL().getGL2();
        // get the OpenGL 2 graphics object
        if(height <=0)
            height =1;
        //preventing devided by 0 exception height =1;
        final float h = (float) width / (float) height;
        // display area to cover the entire window
        // 改变视口
        gl.glViewport(0, 0, width, height);
        //transforming projection matrix
        // 改变投影矩阵
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        //transforming model view gl.glLoadIdentity();
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
}
